#include "spwm_timer0.h"
#include "spwm_int.h"

#include "ddl.h"
#include "bt.h"
#include "gpio.h"
#include "flash.h"
#include "adc.h"

//unsigned short test = 0;
//端口配置初始化
void App_Timer0PortCfg(void)
{
    stc_gpio_cfg_t stcTIM0Port;
    stc_gpio_cfg_t stcLEDPort;

    //结构体初始化清零
    DDL_ZERO_STRUCT(stcTIM0Port);
    DDL_ZERO_STRUCT(stcLEDPort);

    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //GPIO 外设时钟使能

    stcLEDPort.enDir = GpioDirOut;
    Gpio_Init(EVB_LEDR_PORT, EVB_LEDR_PIN, &stcLEDPort); //PD14设置为LED输出

    stcTIM0Port.enDir = GpioDirOut;

    Gpio_Init(GpioPortA, GpioPin0, &stcTIM0Port);
    Gpio_SetAfMode(GpioPortA, GpioPin0, GpioAf7); //PA00设置为TIM0_CHA
    
    Gpio_Init(GpioPortA, GpioPin1, &stcTIM0Port);
    Gpio_SetAfMode(GpioPortA, GpioPin1, GpioAf3); //PA01设置为TIM0_CHB

}

//Timer0 配置
void App_Timer0Cfg(uint16_t u16Period, uint16_t u16CHxACompare, uint16_t u16CHxBCompare)
{
    uint16_t u16CntValue;
    uint8_t u8ValidPeriod;
    stc_bt_mode23_cfg_t stcBtBaseCfg;
    stc_bt_m23_compare_cfg_t stcBtPortCmpCfg;
    stc_bt_m23_dt_cfg_t  stcBtDeadTimeCfg;
    

    //结构体初始化清零
    DDL_ZERO_STRUCT(stcBtBaseCfg);
    DDL_ZERO_STRUCT(stcBtPortCmpCfg);

    Sysctrl_TimerPllClkSel(SysctrlTimerClkPll2);               //定时器时钟倍频
    Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, TRUE); //Base Timer外设时钟使能

    stcBtBaseCfg.enWorkMode = BtWorkMode2;          //改为锯齿波模式  三角波频率减半//三角波模式
    stcBtBaseCfg.enCT = BtTimer;                    //定时器功能，计数时钟为内部PCLK
    stcBtBaseCfg.enPRS = BtPCLKDiv1;                //PCLK
    stcBtBaseCfg.enCntDir = BtCntUp;                //向上计数，在三角波模式时只读
    stcBtBaseCfg.enPWMTypeSel = BtComplementaryPWM; //BtComplementaryPWM;互补  //独立输出PWM
    stcBtBaseCfg.enPWM2sSel = BtSinglePointCmp;     //单点比较功能
    stcBtBaseCfg.bOneShot = FALSE;                  //循环计数
    stcBtBaseCfg.bURSSel = FALSE;                   //上下溢更新

    Bt_Mode23_Init(TIM0, &stcBtBaseCfg); //TIM0 的模式23功能初始化

    Bt_M23_ARRSet(TIM0, u16Period, TRUE); //设置重载值,并使能缓存

    Bt_M23_CCR_Set(TIM0, BtCCR0A, u16CHxACompare); //设置比较值A

    Bt_M23_CCR_Set(TIM0, BtCCR0B, u16CHxBCompare); //设置比较值B

    stcBtPortCmpCfg.enCH0ACmpCtrl = BtPWMMode2;      //OCREFA输出控制OCMA:PWM模式2
    stcBtPortCmpCfg.enCH0APolarity = BtPortPositive; //正常输出
    stcBtPortCmpCfg.bCh0ACmpBufEn = TRUE;            //A通道缓存控制
    stcBtPortCmpCfg.enCh0ACmpIntSel = BtCmpIntNone;  //A通道比较控制:无

    stcBtPortCmpCfg.enCH0BCmpCtrl = BtPWMMode2;      //OCREFB输出控制OCMB:PWM模式2
    stcBtPortCmpCfg.enCH0BPolarity = BtPortPositive; //正常输出
    stcBtPortCmpCfg.bCH0BCmpBufEn = TRUE;            //B通道缓存控制使能
    stcBtPortCmpCfg.enCH0BCmpIntSel = BtCmpIntNone;  //B通道比较控制:无

    Bt_M23_PortOutput_Cfg(TIM0, &stcBtPortCmpCfg); //比较输出端口配置

    u8ValidPeriod = 1;                          //事件更新周期设置，0表示三角波波每半周期更新一次，每+1代表延迟半个周期
    Bt_M23_SetValidPeriod(TIM0, u8ValidPeriod); //间隔周期设置

    u16CntValue = 0;
    Bt_M23_Cnt16Set(TIM0, u16CntValue); //设置计数初值
    
    //设置死区时间
    stcBtDeadTimeCfg.bEnDeadTime = TRUE;
    stcBtDeadTimeCfg.u8DeadTimeValue = 120;
    Bt_M23_DT_Cfg(TIM0, &stcBtDeadTimeCfg);

    Bt_ClearAllIntFlag(TIM0);               //清中断标志
    Bt_Mode23_EnableIrq(TIM0, BtUevIrq);    //使能TIM0 UEV更新中断
    EnableNvic(TIM0_IRQn, IrqLevel0, TRUE); //TIM0中断使能
}

void App_Timer0SetARR(uint16_t u16Period)
{
    Bt_M23_ARRSet(TIM0, u16Period, TRUE); 
}
/*******************************************************************************
 * TIM0中断服务函数
 ******************************************************************************/
void Tim0_IRQHandler(void)
{
    static uint8_t i;
    unsigned short test;
    //Timer0 模式23 更新中断
    if (TRUE == Bt_GetIntFlag(TIM0, BtUevIrq))
    { 
#if 0
        if(0 == i)
        {
            Gpio_WriteOutputIO(EVB_LEDR_PORT, EVB_LEDR_PIN, TRUE);  //LED 引脚输出高电平
            
            Bt_M23_CCR_Set(TIM0, BtCCR0A, 1200); //设置通道A比较值
            Bt_M23_CCR_Set(TIM0, BtCCR0B, 3600); //设置通道B比较值
            
            i++;
        }
        else if(1 == i)
        {
            Gpio_WriteOutputIO(EVB_LEDR_PORT, EVB_LEDR_PIN, FALSE);  //LED 引脚输出低电平
            
            Bt_M23_CCR_Set(TIM0, BtCCR0A, 3600); //设置通道A比较值
            Bt_M23_CCR_Set(TIM0, BtCCR0B, 1200); //设置通道B比较值
            
            i = 0;
        }
#endif
        
				 if (SPWMCnt <= N)
        {
            SPWMCnt = 127;
        }
        else 
        {
            SPWMCnt--;
        }
				test = SPWMCnt;
				 //test = 96;
        Bt_M23_CCR_Set(TIM0, BtCCR0A, sine_Int[test]); //输出sin波
				
        Bt_ClearIntFlag(TIM0, BtUevIrq); //清中断标志
		
    }
}